******************************************************* Merge all the NSS datasets together ******************************************************
local topdir "\nss_64_10"
local topdir2 "\nss_61_10"

******************** round 64 ************************

* level 5 data is unique at activity level of an individual, so need to reshape before merging
use "`topdir'\data_output\level05.dta", clear
reshape wide curr_day_status- special_char_ok_stamp, i(common_id person_srl_no) j(activity_srl_no)
save "`topdir'\data_output\level05_reshaped.dta", replace

* level 7 data is unique at item level of a household, so need to reshape before merging
use "`topdir'\data_output\level07.dta", clear
rename special_char_ok_stamp special_char_ok_stamp_item
reshape wide value_30- special_char_ok_stamp_item, i(common_id) j(item_srl_no)
save "`topdir'\data_output\level07_reshaped.dta", replace

* merge datasets
use "`topdir'\data_output\level01.dta", clear
merge 1:1 common_id using "`topdir'\data_output\level02.dta"
drop _merge
merge 1:m common_id using "`topdir'\data_output\level04.dta"
drop _merge
merge 1:1 common_id person_srl_no using "`topdir'\data_output\level05_reshaped.dta"
drop _merge
merge 1:1 common_id person_srl_no using "`topdir'\data_output\level06.dta"
drop _merge
merge m:1 common_id using "`topdir'\data_output\level07_reshaped.dta"
drop _merge

gen state_code=state
gen dist_code=district

merge m:1 state_code dist_code using "`topdir'\data_output\dist_phase_nregs.dta"
replace nrega_phase_1=0 if nrega_phase_1==.
replace nrega_phase_2=0 if nrega_phase_2==.
drop _merge
save "`topdir'\data_output\merged_all_NSS_NREGA_64.dta", replace


******************** round 61 ************************
* level 6 data is unique at activity level of an individual, so need to reshape before merging
use "`topdir2'\data_output\level06.dta", clear
reshape wide centre_code_round_shift status - blank, i(common_id person_serial_number) j(activity_serial_number)
save "`topdir2'\data_output\level06_reshaped.dta", replace

* merge datasets
use "`topdir2'\data_output\level01.dta", clear
merge 1:m common_id using "`topdir2'\data_output\level03.dta"
drop _merge
merge 1:1 common_id person_serial_number using "`topdir2'\data_output\level04.dta"
drop _merge
merge 1:1 common_id person_serial_number using "`topdir2'\data_output\level06_reshaped.dta"
drop _merge

gen state_code=state
gen dist_code=district
rename centre_code_round_shift centre_code_round_shift_61

merge m:1 state_code dist_code using "`topdir'\data_output\dist_phase_nregs.dta"
replace nrega_phase_1=0 if nrega_phase_1==.
replace nrega_phase_2=0 if nrega_phase_2==.
drop _merge
save "`topdir2'\data_output\merged_all_NSS_NREGA_61.dta", replace


append using "`topdir'\data_output\merged_all_NSS_NREGA_64.dta"
save "`topdir'\data_output\merged_all_NSS_NREGA_61_64.dta", replace


